<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    var a = [1, 3, 6, 3, 23, 76, 1, 34, 222, 6, 456, 221];
    function heap_sort(arr) {
      var len = arr.length,
        k = 0;

      function swap(i, j) {
        var temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
      }
      function max_heapify(start, end) {
        var dad = start;
        var son = dad * 2 + 1;
        if (son >= end) return;
        if (son + 1 < end && arr[son] < arr[son + 1]) {
          son++;
        }
        if (arr[dad] <= arr[son]) {
          swap(dad, son);
          max_heapify(son, end);
        }
      }
      for (var i = Math.floor(len / 2) - 1; i >= 0; i--) {
        max_heapify(i, len);
      }
      for (var j = len - 1; j > k; j--) {
        swap(0, j);
        max_heapify(0, j);
      }
      return arr;
    }
    console.log(heap_sort(a));
  </script>
</html>
